<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket客户端</title>
</head>
<body>

<script type="text/javascript">
    var webSocket;
    if (window.WebSocket) {
        webSocket = new WebSocket("ws://localhost:8088/ws");

        webSocket.onmessage = function (ev) {
            var ta = document.getElementById("responseText");
            ta.value = ta.value + "\n" + ev.data;
        };

        webSocket.onopen = function (ev) {
            var ta = document.getElementById("responseText");
            ta.value = ta.value + "\n" + "连接开启！";
        };

        webSocket.onclose = function (ev) {
            var ta = document.getElementById("responseText");
            ta.value = ta.value + "\n" + "连接关闭！";
        };

        webSocket.onerror = function (ev) {
            var ta = document.getElementById("responseText");
            ta.value = ta.value + "\n" + "未知异常！";
        };
    } else {
        alert("当前浏览器不支持WebSocket！");
    }

    //向服务器端发送信息；
    function sendMsg(message) {
        if (!window.WebSocket) {
            return;
        }
        if (webSocket.readyState === WebSocket.OPEN) {
            webSocket.send(message);
        } else {
            alert("和服务器端连接尚未开启！");
        }
    }



</script>

<form onsubmit="return false;">
    <textarea name="message" style="width: 400px; height: 200px;"></textarea>
    <input type="button" value="发送数据" onclick="sendMsg(this.form.message.value)">

    <h3>服务器端返回：</h3>
    <textarea id="responseText" style="width: 400px;height: 300px;"></textarea>
    <input type="button" onclick="javascript: document.getElementById('responseText').value=''" value="清空内容">
</form>

</body>
</html>